package g11.action;

import g11.dao.DAOException;
import g11.dao.EmployeeDAO;
import g11.dao.EmployeeDAOImpl;
import g11.dao.UserDAOImpl;
import g11.mis.UserTypeEnum;
import g11.model.Employee;
import g11.model.User;

import java.util.Map;

import org.apache.log4j.Logger;
import org.apache.struts2.interceptor.SessionAware;

import com.opensymphony.xwork2.ActionSupport;

public class LoginAction extends ActionSupport implements SessionAware {

	private static final long serialVersionUID = 6066592355639005936L;
	private static final Logger log = Logger.getLogger(LoginAction.class);
	private Map<String, Object> session;
	private String username;
	private String password;
	private User user;
	private String nextActionName;
	private String errorMessage;
	private String roleName;

	public LoginAction() {
		log.info("LoginAction is created");
	}

	public User getUser() {
		return user;
	}

	public void setUser(User user) {
		this.user = user;
	}

	public Map<String, Object> getSession() {
		return session;
	}

	public String getUsername() {
		return username;
	}

	public void setUsername(String username) {
		this.username = username;
	}

	public String getPassword() {
		return password;
	}

	public void setPassword(String password) {
		this.password = password;
	}

	public String getNextActionName() {
		return nextActionName;
	}

	public void setNextActionName(String nextActionName) {
		this.nextActionName = nextActionName;
	}

	public String getErrorMessage() {
		return errorMessage;
	}

	public void setErrorMessage(String errorMessage) {
		this.errorMessage = errorMessage;
	}

	@Override
	public void setSession(Map session) {
		this.session = session;
	}

	public String getRoleName() {
		return roleName;
	}

	public void setRoleName(String roleName) {
		this.roleName = roleName;
	}

	/*
	 * public void createPatient(){ // initiate object Patient patient = new
	 * Patient(); patient.setUsername("zhenyi");
	 * patient.setName("Zhenyi Zhang"); patient.setPassword("0000"); Byte type =
	 * 0; patient.setType(type); patient.setPhone("519-888-8888");
	 * patient.setAddress("77 Weber Street"); patient.setOhip("8392138912");
	 * patient.setSin("123546"); patient.setCurrentHealth("Good."); //to form a
	 * DateTime of joda typee Date date=new Date(); Date myDate = new
	 * java.sql.Date(date.getTime()); patient.setLastVisitTime(myDate);
	 * //patient.setDepartment("Surgery"); //patient.setTitle("Surgery Doctor");
	 * // create start PatientDAO patientDao = new PatientDAOImpl(); try {
	 * patientDao.createPatient(patient); } catch (DAOException e) { // TODO
	 * Auto-generated catch block e.printStackTrace(); } }
	 */

	public String userLogin() {
		log.info("LoginAction.userLogin()");

		try {
			User u = validateUser(username, password);
			this.setUser(u);
			if (u != null) {
				switch (u.getType()) {
				case 0:
					this.setNextActionName("PATIENT");
					break;
				case 1:
					this.setNextActionName("DOCTOR");
					break;
				case 2:
					this.setNextActionName("STAFF");
					break;
				case 3:
					this.setNextActionName("FINANCE");
					break;
				case 4:
					this.setNextActionName("LEGAL");
					break;
				case 5:
					this.setNextActionName("ADMIN");
					break;
				}
				// update session information here
				session.put("currentUser", u);
				session.put("currentUserId", u.getId());
				return SUCCESS;
			} else {
				this.setErrorMessage("Username or password is not correct");
				return ERROR;
			}

		} catch (DAOException e) {
			e.printStackTrace();
			this.setErrorMessage("Database access error");
			return ERROR;
		}
	}

	private User validateUser(String username, String password)
			throws DAOException {

		log.info("in validateUser");
		UserDAOImpl uDAOImpl = new UserDAOImpl();
		User u = null;

		u = uDAOImpl.getUserByUsername(username, password);
		return u;
	}

	public String userLogout() {

		// todo remove session
		this.session.put("currentUser", null);

		return SUCCESS;
	}

	public String backToMainPage() {
		return SUCCESS;
	}
	
	//create admin user 
	public String createAdminUser(){
		Employee admin = new Employee();
		admin.setType((byte) UserTypeEnum.ADMIN.index());
		
		admin.setName("Administrator");
		admin.setUsername("admin");
		admin.setPassword("admin");
		admin.setDepartment("Administration");
		admin.setTitle("admin manager");
		
		/*
		 * persist retrievedEmployee through EmployeeDAO
		 */
		try {
			EmployeeDAO epDao = new EmployeeDAOImpl();
			epDao.createEmployee(admin);
		} catch (DAOException e) {
			log.error("Error: EmployeeAction: "
					+ "createDocotr(): createEmployee", e);
			return ERROR;
		}
		return SUCCESS;
	}

}
